home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1994 October / Macformat17.cdr / Shareware City / Developers / Kamprath's CDEF Pack ƒ / CDEF Sampler Program ƒ / sampler.c < prev    next >
C/C++ Source or Header  |  1994-06-12  |  6KB  |  318 lines

  1. /*****
  2.  * bullseye.c
  3.  *
  4.  *    A simple demonstration program to play with the debugger
  5.  *
  6.  *
  7.  *****/
  8.  
  9. #include "samplerMenus.h"
  10. #include "samplerWindow.h"
  11. #include "samplerControls.h"
  12. #include "number_picker.h"
  13.  
  14. extern    WindowPtr        samplerWindow;
  15. extern    ControlHandle        rocketCntl, timeButtonCntl, timeCntl, dateCntl, numberCntl;
  16. extern    ControlHandle        squarePlain, squareBold, squareLarge;
  17. extern    Rect            dragRect;
  18.  
  19. SysEnvRec    gMac;            
  20.  
  21. void InitMacintosh(void);
  22. void HandleMouseDown (EventRecord    *theEvent);
  23. void HandleEvent(void);
  24. void HandleOSEvent(EventRecord *event);
  25. void DoActivateWindow( Boolean activating );
  26.  
  27. /****
  28.  * InitMacintosh()
  29.  *
  30.  * Initialize all the managers & memory
  31.  *
  32.  ****/
  33.  
  34. void InitMacintosh(void)
  35.  
  36. {
  37.     MaxApplZone();
  38.     
  39.     InitGraf(&thePort);
  40.     InitFonts();
  41.     FlushEvents(everyEvent, 0);
  42.     InitWindows();
  43.     InitMenus();
  44.     TEInit();
  45.     InitDialogs(0L);
  46.     InitCursor();
  47.  
  48.     SysEnvirons(curSysEnvVers, &gMac);
  49.     
  50.     if (gMac.systemVersion < 0x0700)
  51.         ExitToShell();
  52.  
  53. }
  54. /* end InitMacintosh */
  55.  
  56.  
  57. /****
  58.  * HandleMouseDown (theEvent)
  59.  *
  60.  *    Take care of mouseDown events.
  61.  *
  62.  ****/
  63.  
  64. void HandleMouseDown (EventRecord    *theEvent)
  65.  
  66. {
  67.     WindowPtr    theWindow;
  68.     int            windowCode = FindWindow (theEvent->where, &theWindow);
  69.     
  70.     switch (windowCode)
  71.       {
  72.       case inSysWindow: 
  73.         SystemClick (theEvent, theWindow);
  74.         break;
  75.         
  76.       case inMenuBar:
  77.           AdjustMenus();
  78.         HandleMenu(MenuSelect(theEvent->where));
  79.         break;
  80.         
  81.       case inDrag:
  82.           if (theWindow == samplerWindow)
  83.             DragWindow(samplerWindow, theEvent->where, &dragRect);
  84.             break;
  85.             
  86.       case inContent:
  87.           if (theWindow == samplerWindow)
  88.           {
  89.           short            cntlPart;
  90.           Point            clickPt = theEvent->where;
  91.           ControlHandle    foundCntl;
  92.           
  93.               if (theWindow != FrontWindow())
  94.                   SelectWindow(samplerWindow);
  95.                 else
  96.                 {
  97.                     GlobalToLocal(&clickPt);
  98.                     
  99.                   cntlPart = FindControl(clickPt, samplerWindow, &foundCntl);
  100.                   if (cntlPart)
  101.                   {
  102.                       if (( foundCntl == rocketCntl)||( foundCntl == timeButtonCntl ))
  103.                       {
  104.                           if (cntlPart = TrackControl( foundCntl, clickPt, 0 ))
  105.                           {
  106.                               if ( foundCntl == rocketCntl)
  107.                               {
  108.                                   SysBeep(20);
  109.                               }
  110.                               else if (foundCntl == timeButtonCntl)
  111.                               {
  112.                                   SetCtlValue( timeButtonCntl, !GetCtlValue(timeButtonCntl));
  113.  
  114.                                 if (GetCtlValue( timeButtonCntl))
  115.                                 {
  116.                                     HiliteControl(timeCntl, 0);
  117.                                     HiliteControl(dateCntl, 0);
  118.                                     HiliteControl(numberCntl, 0);
  119.                                 }
  120.                                 else
  121.                                 {
  122.                                     HiliteControl(timeCntl, 255);
  123.                                     HiliteControl(dateCntl, 255);
  124.                                     HiliteControl(numberCntl, 255);
  125.                                 }
  126.  
  127.                               }
  128.                           }
  129.                       }
  130.                       else
  131.                       {
  132.                           if ( foundCntl == dateCntl)
  133.                           {
  134.                               SetCtlValue( timeCntl, 0 );
  135.                               HandleDateCDEFClick(clickPt);
  136.                           }
  137.                           else if ( foundCntl == timeCntl)
  138.                           {
  139.                               SetCtlValue( dateCntl, 0 );
  140.                               HandleTimeCDEFClick(clickPt);
  141.                           }
  142.                           else if ( foundCntl == numberCntl)
  143.                           {
  144.                               if (cntlPart = TrackControl( foundCntl, clickPt, NumCDEFProc ))
  145.                               {
  146.                                 /* do nothing because NumCDEFProc() handles it */
  147.                               }
  148.                           }
  149.                           else if ((foundCntl == squarePlain)
  150.                                       ||(foundCntl == squareBold)
  151.                                       ||(foundCntl == squareLarge))
  152.                           {
  153.                               if (cntlPart = TrackControl( foundCntl, clickPt, NumCDEFProc ))
  154.                               {
  155.                                 SetCtlValue( foundCntl, !GetCtlValue( foundCntl ));
  156.                               }
  157.                           }
  158.                       }
  159.                   }
  160.                   else
  161.                   {
  162.                       SetCtlValue( timeCntl, 0 );
  163.                       SetCtlValue( dateCntl, 0 );
  164.                   }
  165.               }
  166.                               
  167.                   
  168.            }
  169.           break;
  170.           
  171.       case inGoAway:
  172.           if (theWindow == samplerWindow && 
  173.               TrackGoAway(samplerWindow, theEvent->where))
  174.           HideWindow(samplerWindow);
  175.             break;
  176.       }
  177. }
  178. /* end HandleMouseDown */
  179.  
  180.  
  181. /****
  182.  * HandleEvent()
  183.  *
  184.  *        The main event dispatcher. This routine should be called
  185.  *        repeatedly (it  handles only one event).
  186.  *
  187.  *****/
  188.  
  189. void HandleEvent(void)
  190.  
  191. {
  192.     int                ok;
  193.     EventRecord        theEvent;
  194.     ControlHandle    curCntl;
  195.  
  196.     HiliteMenu(0);
  197.     SystemTask ();        /* Handle desk accessories */
  198.     
  199.     ok = GetNextEvent (everyEvent, &theEvent);
  200.     if (ok)
  201.         switch (theEvent.what)
  202.         {
  203.             case mouseDown:
  204.                 HandleMouseDown(&theEvent);
  205.                 break;
  206.             
  207.             case keyDown: 
  208.             case autoKey:
  209.                 if ((theEvent.modifiers & cmdKey) != 0)
  210.                 {
  211.                       AdjustMenus();
  212.                       HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
  213.                   }
  214.                 break;
  215.             
  216.             case updateEvt:
  217.                 BeginUpdate(samplerWindow);
  218.                 DrawSampleWindow(((WindowPeek) samplerWindow)->hilited);
  219.                 EndUpdate(samplerWindow);
  220.                 break;
  221.             
  222.             case activateEvt:
  223.                 if (theEvent.modifiers & activeFlag)
  224.                 {
  225.                     DoActivateWindow( true );
  226.                 }
  227.                 else
  228.                 {    
  229.                     DoActivateWindow( false );
  230.                 }
  231.                 break;
  232.             case osEvt:
  233.                 HandleOSEvent(&theEvent);
  234.                 break;
  235.  
  236.         }
  237. }
  238. /* end HandleEvent */
  239.  
  240. void HandleOSEvent(EventRecord *event)
  241. {
  242. Boolean    inBackground;
  243.  
  244.     switch ((event->message >> 24) & 0x00FF) {        /* High byte of message */
  245.         case suspendResumeMessage:
  246.  
  247.             inBackground = (event->message & resumeFlag) == 0;
  248.             if (FrontWindow()) 
  249.             {
  250.                 DoActivateWindow( !inBackground);
  251.             }
  252.             if (!inBackground)
  253.             {
  254.                 SetCursor( &arrow );
  255.             }
  256.             break;
  257.         case mouseMovedMessage:
  258.             break;
  259.     }
  260. }
  261.  
  262. void DoActivateWindow( Boolean activating )
  263. {
  264. ControlHandle  curCntl;
  265.  
  266.     if (activating)
  267.     {
  268.         HiliteControl(rocketCntl, 0);
  269.         HiliteControl(timeButtonCntl, 0);
  270.                     
  271.         if (GetCtlValue( timeButtonCntl))
  272.         {
  273.             HiliteControl(timeCntl, 0);
  274.             HiliteControl(dateCntl, 0);
  275.             HiliteControl(numberCntl, 0);
  276.         }
  277.         else
  278.         {
  279.             HiliteControl(timeCntl, 255);
  280.             HiliteControl(dateCntl, 255);
  281.             HiliteControl(numberCntl, 255);
  282.         }
  283.  
  284.         HiliteControl(squarePlain, 0);
  285.         HiliteControl(squareBold, 0);
  286.         HiliteControl(squareLarge, 0);
  287.                         
  288.     }
  289.     else
  290.     {    
  291.         curCntl = ((WindowPeek)samplerWindow)->controlList;
  292.         while (curCntl)
  293.         {
  294.             HiliteControl(curCntl, 255);
  295.             curCntl = (*curCntl)->nextControl;
  296.         }
  297.     }
  298. }
  299.  
  300. /*****
  301.  * main()
  302.  *
  303.  *    This is where everything happens
  304.  *
  305.  *****/
  306.  
  307.  
  308. main()
  309.  
  310. {
  311.     InitMacintosh();
  312.     SetUpMenus();
  313.     SetUpWindow();
  314.     
  315.     for (;;)
  316.         HandleEvent();
  317. }
  318. /* end main */